package com.core.Interceptor;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.StrutsStatics;
import org.springframework.stereotype.Component;

import com.core.dao.MenuActionFuncDAO;
import com.core.dao.MenuDAO;
import com.core.model.MenuActionFunc;
import com.core.model.Menus;
import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.MethodFilterInterceptor;

/**
 * 登录拦截器
 * 
 * @author pt-chenqw
 *
 */
@Component
public class LoginInterceptor extends MethodFilterInterceptor {

	private static final long serialVersionUID = 1L;
	
	@Resource(name="menuDAO")
	private MenuDAO menuDAO;
	
	@Resource(name="menuActionFuncDAO")
	private MenuActionFuncDAO actionFuncDAO;
	
	@Override
	protected String doIntercept(ActionInvocation invocation) throws Exception {
		ActionContext actionContext = invocation.getInvocationContext();   
        HttpServletRequest request= (HttpServletRequest) actionContext.get(StrutsStatics.HTTP_REQUEST);   
		Object user = actionContext.getSession().get("user");
		String actionName = invocation.getProxy().getActionName();
		if (user != null) {
			Menus menu = menuDAO.getMenusByActionUrl(actionName);
			Menus parentMenu = new Menus();
			String menuName = null;
			Integer parentID = null;
			//为空则说明不是菜单链接,而是菜单功能按钮链接
			if(menu == null){
				MenuActionFunc actionFunc = actionFuncDAO.getMenuActionFuncByActionUrl(actionName);
				parentID = actionFunc.getParentID();
				menuName = actionFunc.getName();
			}else{
				menuName = menu.getMenu_zh_name();
				parentID = menu.getParentID();
			}
			parentMenu = menuDAO.getMenuByMenuID(parentID);
			request.setAttribute("currentMenuName", menuName);
			request.setAttribute("currentParentMenu", parentMenu);
			return invocation.invoke();
		}
		return Action.LOGIN;
	}

}
